package cn.stylefeng.guns.modular.filerun.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.filerun.model.FileParam;
import cn.stylefeng.guns.modular.filerun.model.FileResult;
import cn.stylefeng.guns.modular.filerun.model.FileUploadResult;
import cn.stylefeng.guns.modular.filerun.service.FileImgService;
import cn.stylefeng.guns.modular.filerun.service.FileRunService;
import cn.stylefeng.guns.sys.modular.system.entity.FileImgPO;
import cn.stylefeng.guns.sys.modular.system.entity.FilePO;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Controller
@RequestMapping("/fileRun")
public class FileRunController extends BaseController {

    private String PREFIX = "/fileRun";

    @Autowired
    private FileRunService fileRunService;
    @Autowired
    private FileImgService fileImgService;


    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/stuManage.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/stuManage_add.html";
    }

    /**
     * 修改页面
     */
    @RequestMapping("/fileRun_update")
    public String deptUpdate() {
        return PREFIX + "/stuManage_edit.html";
    }

    /**
     * 新增接口
     *
     * @author stylefeng
     * @Date 2019-02-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FileParam fileParam) {
        FilePO filePO = new FilePO();
        BeanUtils.copyProperties(fileParam,filePO);
        fileRunService.addFile(filePO);
        return ResponseData.success();
    }

    /**
     * 查询列表
     * @author stylefeng
     * @Date 2019-02-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FileParam fileParam) {
        Page page = LayuiPageFactory.defaultPage();
        List<FileResult> results = fileRunService.processList(page, fileParam);
        page.setRecords(results);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 文件详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(Long fId) {
        FilePO filePO = fileRunService.getById(fId);
        Map<String, Object> noticeMap = BeanUtil.beanToMap(filePO);
        return ResponseData.success(noticeMap);
    }

    /**
     * 修改文件
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public ResponseData update(FileParam fileParam) {
        boolean b = fileRunService.updateRecord(fileParam);
        if (b) {
            return ResponseData.success(200, "修改成功", null);
        }
        return ResponseData.success(500, "修改失败", null);
    }

    /**
     * 删除记录（删除）
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FileParam fileParam) {
        //删除该位置得fsdf的旧文件
        if(!StringUtils.isEmpty(fileParam.getFUrl())){
            fileRunService.deleteFile(fileParam.getFUrl());
        }
        boolean b = fileRunService.removeById(fileParam.getFId());
        if (b) {
            return ResponseData.success(200, "删除成功", null);
        }
        return ResponseData.success(500, "删除失败", null);
    }

    /**
     * 源码上传接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/upload")
    @ResponseBody
    public ResponseData UploadToFdfs(@RequestPart("file") MultipartFile file) {
        String fileUrl = fileRunService.uploadFile(file);
        if(!StringUtils.isEmpty(fileUrl)){
            HashMap<String, Object> map = new HashMap<>();
            map.put("fileUrl", fileUrl);
            return ResponseData.success(0, "上传成功", map);
        }
        return ResponseData.success(0, "上传失败，请重试", null);
    }

    /**
     * 图片上传接口
     */
    @RequestMapping(method = RequestMethod.POST, path = "/uploads")
    @ResponseBody
    public ResponseData UploadToOss(@RequestParam(value = "file", required = false) MultipartFile file) {
        FileUploadResult fileUploadResult = fileImgService.uploadFileByOss(file);
        if (!StringUtils.isEmpty(fileUploadResult.getName())) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("images", fileUploadResult.getName() + "_");
            return ResponseData.success(0, "上传成功", map);
        }
        return ResponseData.success(500, "上传失败", null);
    }

    //记录该记录下载次数
    @RequestMapping(method = RequestMethod.POST, path = "/upCount")
    @ResponseBody
    public void upCount(Long fId) {
        FilePO filePO = fileRunService.getById(fId);
        filePO.setfDownCount(filePO.getfDownCount()+1);
        fileRunService.updateById(filePO);
    }
}


